using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Searcher
{
    internal partial class frmDocPreview : Form
    {
        private static readonly String m_PathToDocPreviewCSS = String.Format("file:///{0}/Data/doc.css", Application.StartupPath.Replace('\\', '/'));
        private Searcher m_Searcher;

        public Int32 DocumentID { get { return m_Searcher.GetDocumentIDByName(lbDoc.Items[lbDoc.SelectedIndex].ToString()); } }
        public String DocumentName { get { return lbDoc.Items[lbDoc.SelectedIndex].ToString(); } }

        public frmDocPreview(Searcher searcher)
        {
            InitializeComponent();

            m_Searcher = searcher;
        }

        private void frmDocPreview_Load(object sender, EventArgs e)
        {
            lbDoc.Items.AddRange(m_Searcher.GetDocuments().ToArray());
        }

        private void lbDoc_Click(object sender, EventArgs e)
        {
        }

        private void lbDoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbDoc.SelectedIndex == -1)
            {
                return;
            }

            btOK.Enabled = true;
            String doc_name = lbDoc.Items[lbDoc.SelectedIndex].ToString();
            StringBuilder docPreviewText = new StringBuilder(String.Format("<html><header><style type=\"text/css\">@import \"{0}\";</style></header><body>", m_PathToDocPreviewCSS));
            docPreviewText.Append(m_Searcher.GetHtmlDocument(m_Searcher.GetDocumentIDByName(doc_name), 0, 100, -1, -1));
            docPreviewText.Append("</body></html>");
            wbDocPreview.DocumentText = docPreviewText.ToString();
        }
    }
}